package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.dao.UserMapper;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 功能描述：
 *
 * @Author: Zhanghf
 * @Date: 2021/5/30 13:39
 */
@Controller
public class SettingController {
    @Autowired(required = false)
    private UserMapper userMapper;
@PostMapping("setting")
    public String setting(HttpServletResponse response, HttpServletRequest request, Model model, HttpSession session)
{
    User user = (User) session.getAttribute("user");
    model.addAttribute("user",user);
    return "personsetting";
}
    @PostMapping("basesetting")
    public String basesetting(HttpServletResponse response, HttpServletRequest request, Model model, HttpSession session)
    {
        String name = request.getParameter("name");
        String password = request.getParameter("password");

        User user = (User) session.getAttribute("user");

        Integer id = user.getId();
        if (userMapper.selectOne(new QueryWrapper<User>().eq("name", name))!=null)
        {
            String tips="用户名已存在，请重新输入";
            model.addAttribute("tips",tips);
            model.addAttribute("user",user);
            return "basesetting";
        }
         userMapper.update(null, new UpdateWrapper<User>().eq("id", id).set("name", name).set("password", password));


        User newUser = userMapper.selectOne(new QueryWrapper<User>().eq("id", id));

        session.removeAttribute("user");
        session.setAttribute("user",newUser);
        model.addAttribute("user",newUser);
        return "basesetting";
    }
    @PostMapping("picsetting")
    public String picsetting(HttpServletResponse response, HttpServletRequest request, Model model, HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        return "picsetting";
    }
}
